package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.text.SimpleDateFormat;


import com.example.myapplication.databinding.ActivityMainBinding;

import com.example.myapplication.fragment.Tianqi1Fragment;
import com.example.myapplication.fragment.Tianqi2Fragment;
import com.example.myapplication.fragment.Tianqi3Fragment;
import com.example.tqlibrary.MyThread;
import com.example.tqlibrary.javabean.databean;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    MyThread myThread;
    MyHandler myHandler;


    Tianqi1Fragment tianqi1Fragment;
    Tianqi2Fragment tianqi2Fragment;
    Tianqi3Fragment tianqi3Fragment;
    List<Fragment> fragmentList;
    List<databean.HeWeather6Bean> hourlyBeanList;
    List<databean.HeWeather6Bean> nowBeanList;
    List<databean.HeWeather6Bean> forecastBeanList;
    List<databean.HeWeather6Bean> lifestyleBeanList;
    String addr;
    ActivityMainBinding mainBinding;



    class MyHandler extends Handler{
        @Override
        //此处为主页面的上方内容，并添加三个Fragment
        public void handleMessage(@NonNull Message msg) {
            Object data[] = (Object[]) msg.obj;
            hourlyBeanList = (List<databean.HeWeather6Bean>) data[0];
            nowBeanList = (List<databean.HeWeather6Bean>)data[1];
            forecastBeanList = (List<databean.HeWeather6Bean>)data[2];
            lifestyleBeanList = (List<databean.HeWeather6Bean>)data[3];

            mainBinding.textView.setText(nowBeanList.get(0).getNow().getTmp()+"°");
            mainBinding.textViewa.setText(addr);
            mainBinding.textView3.setText(nowBeanList.get(0).getNow().getWind_dir()+nowBeanList.get(0).getNow().getWind_sc()+"级");
            mainBinding.textView2.setText(nowBeanList.get(0).getNow().getCond_txt()+"");
            mainBinding.textView5.setText("气压"+nowBeanList.get(0).getNow().getPres()+"hPa");
            mainBinding.textView8.setText("湿度"+nowBeanList.get(0).getNow().getHum()+"%");
            if(addr == null)
            {
                mainBinding.textViewa.setText("北京");//设立默认值北京
            }

            tianqi1Fragment = new Tianqi1Fragment(hourlyBeanList);
            tianqi2Fragment = new Tianqi2Fragment(lifestyleBeanList);
            tianqi3Fragment = new Tianqi3Fragment(forecastBeanList);
            fragmentList = new ArrayList<>();
            fragmentList.add(tianqi1Fragment);
            fragmentList.add(tianqi2Fragment);
            fragmentList.add(tianqi3Fragment);
            mainBinding.viewpager.setAdapter(new FragmentStateAdapter(MainActivity.this) {
                @NonNull
                //返回现在的fragment
                @Override
                public Fragment createFragment(int position) {
                    return fragmentList.get(position);
                }
                //有多少个fragment
                @Override
                public int getItemCount() {
                    return fragmentList.size();
                }
            });

            final String[] tabText=new String[]{"逐小时预报", "生活指南","五日天气预报"};
            TabLayoutMediator mediator = new TabLayoutMediator(mainBinding.tablayout, mainBinding.viewpager,
                    new TabLayoutMediator.TabConfigurationStrategy() {
                        @Override
                        public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                            tab.setText(tabText[position]);
                        }
                    });
            mediator.attach();
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mainBinding =ActivityMainBinding.inflate( LayoutInflater.from( this ) );
        setContentView(mainBinding.getRoot());

        myHandler = new MyHandler();
        myThread = new MyThread(myHandler,addr);
        myThread.start();


        mainBinding.button.setOnClickListener(new View.OnClickListener() {//加入监听器用于对查询城市天气
            @Override
            public void onClick(View v) {

                //确认为合法输入还是正常输入
                addr = mainBinding.editTextTextPassword2.getText().toString();
                if(TextUtils.isEmpty(addr)){
                    Toast.makeText(getApplicationContext(),
                            "没有输入内容，请重新输入",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(getApplicationContext(),
                            "查找成功,祝你使用过愉快",Toast.LENGTH_SHORT).show();
                }


                myHandler = new MyHandler();
                myThread = new MyThread(myHandler,addr);
                myThread.start();
            }
        });
    }
}

